package framework.jackson.custom;

import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;

public class JsonViewTest {
    private static final ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) throws Exception {
        Obk.B innerObj = new Obk.B("value_g", "value_h");
        Obk obj = new Obk("value_a", innerObj, "value_c");

        // 不序列化 InnerObj.g：{"a":"value_a","b":{"h":"value_h"},"c":"value_c"}
        String value = mapper.writerWithView(Views.Public.class).writeValueAsString(obj);
        System.out.println(value);

        // 返回 Internal，同时继承了 Public
        // 序列化全部属性：{"a":"value_a","b":{"g":"value_g","h":"value_h"},"c":"value_c"}
        System.out.println(mapper.writerWithView(Views.Internal.class).writeValueAsString(obj));
    }

    @Data
    @AllArgsConstructor
    public static class Obk {
        @JsonView(Views.Public.class)
        private String a;

        @JsonView(Views.Public.class)
        private B b;

        @JsonView(Views.Public.class)
        private String c;

        @Data
        @AllArgsConstructor
        public static class B {
            @JsonView(Views.Internal.class)
            private String g;

            @JsonView(Views.Public.class)
            private String h;
        }
    }

    public interface Views {
        interface Public {}
        interface Internal extends Public {}
    }
}
